package com.ruoyi.web.controller.rest.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.WebUser;
import com.ruoyi.system.domain.WebUserWithdraw;
import com.ruoyi.system.service.IWebUserService;
import com.ruoyi.system.service.IWebUserWithdrawService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 微信公众号菜单 信息操作处理
 *
 * @author liupan
 * @date 2019-03-25
 */
//@Api(tags = "用户提现")
//@RestController
//@RequestMapping("/rest/user/withdraw")
public class _UserWithdrawController extends BaseController {
    @Autowired
    private IWebUserWithdrawService iWebUserWithdrawService;
    @Autowired
    private IWebUserService iWebUserService;


    @ApiOperation(value = "用户提现记录",notes = "列表显示的内容：金额amount，当前余额current_total，提现类型：bindType:1支付宝 2银行卡 3微信，如果是支付宝显示支付宝字段alipay_no，如果是银行卡，显示卡号bank_no，打款时间:remit_time,status：0审核中 1已打款 2拒绝打款")
    @ApiResponses({@ApiResponse(code = 200, message = "成功", response = WebUserWithdraw.class)
    })
    @GetMapping()
    public AjaxResult list(Integer userId){
        WebUser userData = iWebUserService.getById(userId);
        if (userData == null) {
            return error(Constants.REQUIRED_FAILD);
        }
//        IPage<WebUserWithdraw> ipage = new Page<WebUserWithdraw>(1, 100);
//        IPage<WebUserWithdraw> data = iWebUserWithdrawService.page(ipage,new QueryWrapper<WebUserWithdraw>().eq("user_id",userId));
        List<WebUserWithdraw> data = iWebUserWithdrawService.list(new QueryWrapper<WebUserWithdraw>().eq("user_id",userId)
                .select("id,amount,current_total,bind_type,bank_no,alipay_no,remit_time,status,create_time").orderByDesc("create_time"));
        return AjaxResult.success(data);
    }

    @ApiOperation(value = "保存用户提现")
    @PostMapping()
    public AjaxResult save(@Validated @RequestBody WebUserWithdraw webUserWithdraw) {
        WebUser userData = iWebUserService.getById(webUserWithdraw.getUserId());
        if (userData == null) {
            return error(Constants.REQUIRED_FAILD);
        }
        if(webUserWithdraw.getAmount() == null || webUserWithdraw.getAmount() <= 0){
            return error("提现金额不能低于0");
        }

        iWebUserWithdrawService.saveWithdraw(webUserWithdraw,userData);
        return AjaxResult.success();
    }

}
